Add a "Clone" button in agents#show.

- Make agents#new take an optional :id for creating a clone of an
  existing agent of the user.

- Delay the call for $(".select2").select2() so filled-in values of
  sources can take effect.

Akinori MUSHA 11 years ago
parent
commit
47fb2894a1

+ 3 - 3
app/assets/javascripts/application.js.coffee.erb

@@ -56,9 +56,6 @@ $(document).ready ->
56 56
   # JSON Editor
57 57
   window.jsonEditor = setupJsonEditor()
58 58
 
59
-  # Select2 Selects
60
-  $(".select2").select2(width: 'resolve')
61
-
62 59
   # Flash
63 60
   if $(".flash").length
64 61
     setTimeout((-> $(".flash").slideUp(-> $(".flash").remove())), 5000)
@@ -155,6 +152,9 @@ $(document).ready ->
155 152
 
156 153
   $("#agent_type").change() if $("#agent_type").length
157 154
 
155
+  # Select2 Selects
156
+  $(".select2").select2(width: 'resolve')
157
+
158 158
   if $(".schedule-region")
159 159
     if $(".schedule-region").data("can-be-scheduled") == true
160 160
       showSchedule()

+ 7 - 1
app/controllers/agents_controller.rb

@@ -71,7 +71,13 @@ class AgentsController < ApplicationController
71 71
   end
72 72
 
73 73
   def new
74
-    @agent = current_user.agents.build
74
+    agents = current_user.agents
75
+
76
+    if id = params[:id]
77
+      @agent = agents.build_clone(agents.find(id))
78
+    else
79
+      @agent = agents.build
80
+    end
75 81
 
76 82
     respond_to do |format|
77 83
       format.html

+ 1 - 0
app/views/agents/show.html.erb

@@ -18,6 +18,7 @@
18 18
           <% end %>
19 19
           <li><%= link_to '<i class="icon-chevron-left"></i> Back'.html_safe, agents_path %></li>
20 20
           <li><%= link_to '<i class="icon-pencil"></i> Edit'.html_safe, edit_agent_path(@agent) %></li>
21
+          <li><%= link_to '<i class="icon-plus"></i> Clone'.html_safe, new_agent_path(id: @agent) %></li>
21 22
 
22 23
           <% if @agent.can_be_scheduled? || @agent.events.count > 0 %>
23 24
             <li class="dropdown">

+ 16 - 0
spec/controllers/agents_controller_spec.rb

@@ -46,6 +46,22 @@ describe AgentsController do
46 46
     end
47 47
   end
48 48
 
49
+  describe "GET new with :id" do
50
+    it "opens a clone of a given Agent" do
51
+      sign_in users(:bob)
52
+      get :new, :id => agents(:bob_website_agent).to_param
53
+      assigns(:agent).attributes.should eq(users(:bob).agents.build_clone(agents(:bob_website_agent)).attributes)
54
+    end
55
+
56
+    it "only allows the current user to clone his own Agent" do
57
+      sign_in users(:bob)
58
+
59
+      lambda {
60
+        get :new, :id => agents(:jane_website_agent).to_param
61
+      }.should raise_error(ActiveRecord::RecordNotFound)
62
+    end
63
+  end
64
+
49 65
   describe "GET edit" do
50 66
     it "only shows Agents for the current user" do
51 67
       sign_in users(:bob)